﻿using HR.Config;
using HR.IService;
using HR.Models;

namespace HR.Service
{
    public class PublicConfigService : IPublicConfigService
    {
        private readonly HRDBContext db;

        public PublicConfigService(HRDBContext db)
        {
            this.db = db;
        }

        public Task<int> PublicConfigAdd(config_public_char cpc)
        {
            return Task.Run(() =>
            {
                db.config_public_chars.Add(cpc);
                return db.SaveChangesAsync();

            });
        }

        public Task<int> PublicConfigDelete(short pbc_id)
        {
            return Task.Run(() =>
            {
                config_public_char? cpc = db.config_public_chars.Find(pbc_id);
                db.config_public_chars.Remove(cpc);
                return db.SaveChangesAsync();
            });
        }

        public Task<Paging<config_public_char>> PublicConfigFill(int pgsize, int pgcurrent)
        {
            return Task.Run(() =>
            {
                List<config_public_char> list = db.config_public_chars.ToList();
                List<config_public_char> tabs = list.Skip((pgcurrent-1)*pgsize).Take(pgsize).ToList();
                Paging<config_public_char> pg = new Paging<config_public_char>()
                {
                    tabs = tabs,
                    rows=list.Count
                };
                return pg;
            });
        }

        public Task<int> PublicConfigUpdate(config_public_char cpc)
        {
            return Task.Run(() =>
            {
                config_public_char? cfpc = db.config_public_chars.Find(cpc.pbc_id);
                cfpc.attribute_name = cpc.attribute_name;
                cfpc.attribute_kind = cpc.attribute_kind;
                return db.SaveChangesAsync();
            });
        }
    }
}
